/**
 * ...
 * @author Rob Dravenstott
 * 
 * Configuration dialogue box to allow user updates to parameters
 * 
 */

import com.chumby.util.Delegate; 
class TedConfig 
{
	private var _app_root:MovieClip;
	private var savenewsettingsFunc:Function;
	private var cp:MovieClip = null;
	
	public var _ted_address:String = "192.168.1.44";
	public var _refresh_time:Number = 2000;
	

	public function TedConfig (app_root:MovieClip, settings:Object, saveNewSettings:Function)
	{
		_app_root = app_root;
		this.savenewsettingsFunc = saveNewSettings;
		
		if (settings.ted_address != undefined)
			_ted_address = String(settings.ted_address);
		if (settings.refresh_time != undefined)
			_refresh_time = Number(settings.refresh_time);
		
		renderGUI();
	}
	
	private function renderGUI()
	{
		cp = _app_root.createEmptyMovieClip("cp", _app_root.getNextHighestDepth());
		
		// Set background color
		GraphicUtils.createRect(cp, "background", 0, 0, 320, 240, 0x000000, 100);

		var tf:TextFormat = new TextFormat();
		tf.align = "center";
		tf.color = 0xFFFFFF; //white text
		tf.bold = true;
		tf.underline = false;
		tf.font = "_sans"; //also _serif and _typewriter available for chumby

		GraphicUtils.createRect(cp, "top_banner", 0, 0, 320, 35, 0xFF33FF, 50);
		
		cp.createTextField("banner_title", this.cp.getNextHighestDepth(), 10, 5, 310, 35);
		tf.size = 20;
		cp.banner_title.setNewTextFormat(tf);
		cp.banner_title.text = "TED IP Address Setup";

		cp.createTextField("cancel_txt", this.cp.getNextHighestDepth(), 283, 5, 35, 35);
		tf.size = 20;
		cp.cancel_txt.setNewTextFormat(tf);
		cp.cancel_txt.text = "X";

		GraphicUtils.createRect(cp, "cancel", 283, 2, 35, 30, 0x000000, 0, 2);
		
		cp.createTextField("banner2_title", this.cp.getNextHighestDepth(), 10, 40, 310, 35);
		tf.size = 12;
		cp.banner2_title.setNewTextFormat(tf);
		cp.banner2_title.text = "Please enter the address of your TED 5000";

		cp.createTextField("ted_address1_bottom", cp.getNextHighestDepth(), 5, 68, 180, 30);
		tf.size = 20;
		tf.align = "right";
		cp["ted_address1_bottom"].setNewTextFormat(tf);
		cp["ted_address1_bottom"].type = "input";
		cp["ted_address1_bottom"].text = _ted_address;
		cp["ted_address1_bottom"].background = true;
		cp["ted_address1_bottom"].backgroundColor = 0x999999
		
		var starting_circles_x = 25;
		var starting_circles_y = 140;
		var between_circles_width = 48;
		var between_circles_height = 50;

		GraphicUtils.createCircleText(cp, "<", starting_circles_x+4*between_circles_width, 85, 20, 0xFF33FF, 50, Delegate.create(this, this.pressback));
		GraphicUtils.createCircleText(cp, "1", starting_circles_x, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.press1));
		GraphicUtils.createCircleText(cp, "2", starting_circles_x+1*between_circles_width, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.press2));
		GraphicUtils.createCircleText(cp, "3", starting_circles_x+2*between_circles_width, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.press3));
		GraphicUtils.createCircleText(cp, "4", starting_circles_x+3*between_circles_width, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.press4));
		GraphicUtils.createCircleText(cp, "5", starting_circles_x + 4 * between_circles_width, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.press5));		
		GraphicUtils.createCircleText(cp, ".", starting_circles_x+5*between_circles_width, starting_circles_y, 20, 0xFF33FF, 50, Delegate.create(this, this.pressdot));

		GraphicUtils.createCircleText(cp, "6", starting_circles_x, starting_circles_y+between_circles_height, 20, 0xFF33FF, 50, Delegate.create(this, this.press6));
		GraphicUtils.createCircleText(cp, "7", starting_circles_x+1*between_circles_width, starting_circles_y+between_circles_height, 20, 0xFF33FF, 50, Delegate.create(this, this.press7));
		GraphicUtils.createCircleText(cp, "8", starting_circles_x+2*between_circles_width, starting_circles_y+between_circles_height, 20, 0xFF33FF, 50, Delegate.create(this, this.press8));
		GraphicUtils.createCircleText(cp, "9", starting_circles_x+3*between_circles_width, starting_circles_y+between_circles_height, 20, 0xFF33FF, 50, Delegate.create(this, this.press9));
		GraphicUtils.createCircleText(cp, "0", starting_circles_x + 4 * between_circles_width, starting_circles_y + between_circles_height, 20, 0xFF33FF, 50, Delegate.create(this, this.press0));
		
		GraphicUtils.createRect(cp, "disclaimer", 0, 220, 320, 20, 0xFF33FF, 50);
		
		cp.createTextField("bottom_message", this.cp.getNextHighestDepth(), 0, 220, 320, 20);
		tf.size = 12;
		tf.align = "center";
		tf.underline = false;
		cp.bottom_message.setNewTextFormat(tf);
		cp.bottom_message.text = "";
		
		this.cp.createTextField("close_btn_title", this.cp.getNextHighestDepth(), 252, 177, 66, 60);
		tf.size = 18;
		tf.align = "center";
		this.cp.close_btn_title.setNewTextFormat(tf);
		this.cp.close_btn_title.text = "Save";

		GraphicUtils.createRect(cp, "close_btn", 251, 170, 66, 40, 0xFF33FF, 50, 2);

		this.cp.cancel.onRelease = Delegate.create(this, this.cancelCP);
		this.cp.close_btn.onPress = Delegate.create(this, this.savePress);
		this.cp.close_btn.onRelease = Delegate.create(this, this.saveCP);
	}
	public function pressback ():Void
	{
		cp["ted_address1_bottom"].text = cp["ted_address1_bottom"].text.slice(0, cp["ted_address1_bottom"].text.length - 1);
	}
	
	public function press0 ():Void
	{
		cp["ted_address1_bottom"].text += "0";
	}
	
	public function press1 ():Void
	{
		cp["ted_address1_bottom"].text += "1";
	}
	public function press2 ():Void
	{
		cp["ted_address1_bottom"].text += "2";
	}
	public function press3 ():Void
	{
		cp["ted_address1_bottom"].text += "3";
	}
	public function press4 ():Void
	{
		cp["ted_address1_bottom"].text += "4";
	}
	public function press5 ():Void
	{
		cp["ted_address1_bottom"].text += "5";
	}
	public function press6 ():Void
	{
		cp["ted_address1_bottom"].text += "6";
	}
	public function press7 ():Void
	{
		cp["ted_address1_bottom"].text += "7";
	}
	public function press8 ():Void
	{
		cp["ted_address1_bottom"].text += "8";
	}
	public function press9 ():Void
	{
		cp["ted_address1_bottom"].text += "9";
	}
	public function pressdot ():Void
	{
		cp["ted_address1_bottom"].text += ".";
	}
	
	public function closeSettings ():Void 
	{
		this.cp.removeMovieClip();
	}

	private function cancelCP ():Void
	{
	
		var settings:Object = { ted_address:cp["ted_address1_bottom"].text, refresh_time:String(this._refresh_time) };
		
		this.savenewsettingsFunc(settings, false);
	}
	
	private function savePress ():Void 
	{
		var myColor = new Color(this.cp.close_btn);
		myColor.setRGB(0x777777);
		showTextMessage("Saving...", 14);
	}
	
	private function saveCP ():Void 
	{
		var myColor = new Color(this.cp.close_btn);
		myColor.setRGB(0xFF33FF);

		var settings:Object = { ted_address:cp["ted_address1_bottom"].text, refresh_time:String(this._refresh_time) };
		
		this.savenewsettingsFunc(settings, true);
	}

	public function showTextMessage (myText:String, myTextSize:Number):Void 
	{
		cp.bottom_message.text = myText;
	}
	
}